﻿using MongoDB.Driver;

namespace ZilLion.Core.MongoDbManager.ObjectDb
{
    public class ObjectDbRepository : MongoDbRepository, IObjectDbRepository
    {
        public MongoServer MongoServer { get; set; }

        public void SaveObjectToMongoDb<T>(T item)
        {
            if (MongoServer == null) return;
            var db = MongoServer.GetDatabase(Context.DatabaseName);
            var collection = db.GetCollection(Context.RootName);
            collection.Insert(item);
        }

        public void InitGridFs()
        {
            if (Context != null)
            {
                MongoServerSettings settings = null;
                MongoServerAddress mongoServerAddress;
                if (MongoServerAddress.TryParse(Context.ServerConnectString, out mongoServerAddress))
                    settings = new MongoServerSettings
                    {
                        Server = mongoServerAddress,
                        MaxConnectionPoolSize = 500, //最大连接池数量
                        WaitQueueSize = 10 //等待列队数量
                    };
                if (settings != null)
                    MongoServer = new MongoServer(settings);
            }
        }
    }
}